﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System.Configuration;
using LN.WebFileManager;
using WebApp.Code;
using System.Security.Principal;
using WindowsSignInPage;

namespace WebApp.Controls
{
    public partial class FileManager : System.Web.UI.UserControl
    {
        LoginUser loginUser;
        Profile userProfile;

        public string Domain
        {
            get
            {
                return "ceibs.edu";
            }
        }
       
        public string UserName
        {
            get
            {
                return loginUser.LoginId;
            }
        }

        public string Password
        {
            get
            {
                return EncryptTool.Decrypt(loginUser.Pwd);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            try
            {
                string currentUser = Authentication.GetCurrentUser();
                if (string.IsNullOrEmpty(currentUser))
                {
                    string logonUrl = Authentication.GetLogonUrl();
                    Response.Redirect(logonUrl);
                }

                loginUser = new LoginUser(currentUser);
               
                if (!WindowsContext.Instance.ImpersonateValidUser(UserName, Domain, Password))
                {
                    lblMessage.Text = "no user";
                }
                else
                {
                    lblMessage.Text = WindowsIdentity.GetCurrent().Name;
                    ADEntry adEntry = new ADEntry(UserName, Password);
                    userProfile = adEntry.UserProfile;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Login user error:" + string.Format("Server:{0},UserName:{1},Pwd:{2}", Domain, UserName, Password), ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    if (userProfile.NetDrives != null && userProfile.NetDrives.Count > 0)
                    {
                        foreach (var item in userProfile.NetDrives)
                        {
                            //lblMessage.Text = item.DriveName + "," + item.DrivePath;
                            fileExplorer.RootDirectories.Add(new RootDirectory()
                            {
                                DirectoryPath = item.DrivePath,
                                ShowRootIndex = false,
                                Text = item.DriveName,
                                ExpandDepth = 1
                            });
                        }
                    }
                    else
                    {
                        litMessage.Text = "Your don't have any network drive currently.";
                    }
                   
                }
                catch (Exception ex)
                {
                    lblMessage.Text = string.Format("Server:{0},UserName:{1},Pwd:{2}", Domain, UserName, Password);
                    lblMessage.Text += "<br/>" + ex.Message + "<br/>" + ex.StackTrace;
                    Logger.Error("Login user error:"+string.Format("Server:{0},UserName:{1},Pwd:{2}", Domain, UserName, Password),ex);
                }

            }
            //if(Page.Request.HttpMethod.ToUpper()=="POST")
            //{
            //    SaveFiles();
            //}
        }

        private void SaveFiles()
        {
            string path=Request.Params["path"];
            //path.Replace(item
            string dir = fileExplorer.CurrentDirectory.PhysicalPath + path;

            for (var i = 0; i < Page.Request.Files.Count; i++)
            {
                var postedFile = Page.Request.Files[i];
                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    ProcessFileUpload(dir, postedFile);
                }
            }
        }

        private void ProcessFileUpload(string dir, System.Web.HttpPostedFile uploadedFile)
        {
            uploadedFile.SaveAs(dir);
        }


        
       
    }

    
   
}